
import layout from './views/layout';
import entry from './views/entry';

import { getLocalStorageItem, setLocalStorageItem, encrypt } from './functions';

// 转换为VueRouter路由配置
function toRoutes(menus) {
    let routes = [];

    // 循环菜单集合
    for (let i = 0, len = menus.length; i < len; i++) {
        const menu = menus[i];

        // 重命名菜单键名
        menu.id = menu.menuId, menu.label = menu.name, menu.path = menu.url, menu.children = menu.childs;
        delete menu.menuId, delete menu.name, delete menu.url, delete menu.childs;

        // 如果菜单有路径则设置对应路径组件
        if (menu.path) {
            menu.component = function () {
                return import('./views' + menu.path);
            };

            // 删除对应权限子集
            delete menu.children;

            // 推入路由集合中
            routes.push(menu);

            continue;
        }

        // 如果菜单有子集则递归执行函数
        if (menu.children) {
            routes.push(...toRoutes(menu.children));

            continue;
        }
    }

    return routes;
}


export default {
    data: {
        // 是否已登录
        isLoggedIn: !!getLocalStorageItem('token').access_token,

        // 侧边栏路由
        sidebarRoutes: null,

        username: '',

    },

    components: { layout, entry },

    methods: {

        // 登入
        logIn(username, password) {
            const authorization = `O1+q67z4TzWXcmzK9oOnZAFFL+ncOkZL6Eo/qGciAy2TNRre4f2xg2dKaCSOjp5QB8thf2+Dya5l9Ek3ejLH46fa+YFa83kK8aX65T1QR7tTJTxH8kIatp6E7TlxNbGUG54HgpDp8svKftLzhXL7hcue8P00R/ILzZ3fUyTPAHQ=`;
            password = encrypt(password);

            this.$post('/yxl/authentication/rsa', { username, password }, {
                headers: { authorization }
            }).then(res => {
                this.$notify.success({
                    title: '成功通知',
                    message: '登录成功',
                });

                setLocalStorageItem('token', res);

                this.isLoggedIn = true;
            });
        },

        // 登出
        logOut() {
            this.$post('/yxl/manager/signOutLogin').then(res => {
                this.$notify.success({
                    title: '成功通知',
                    message: res.msg,
                });

                setLocalStorageItem('token');

                this.isLoggedIn = false;
            });
        },

        // 获取管理员信息
        getAdministratorInfo() {
            this.$get('/yxl/manager/findUserInfo').then(res => {
                this.username = `${res.loginName}/${res.depts}`;
            });
        },

        // 获取管理员对应路由
        getAdministratorRoutes() {
            this.$get('/yxl/menu/findManagerMenu').then(res => {

                // >>>>> 路由操作 <<<<<

                const router = this.$router;
                // 结果转换为路由对象
                const routes = toRoutes(res);
                // 侧边栏路由连接首页路由
                this.sidebarRoutes = Object.freeze([router.options.routes[0]].concat(res));

                // 类别父级路由
                const categoryRoute = {
                    path: '/category',
                    component() {
                        return import('./views/category');
                    },
                    children: [],
                }

                // 分类路由子级id集合
                const categoryRouteChildIdSet = new Set([115, 117, 118, 52, 53]);

                for (let i = 0; i < routes.length; i++) {
                    const route = routes[i];

                    // 如果是类别子路由
                    if (categoryRouteChildIdSet.has(route.id)) {

                        // 拆分的路径
                        let splitPath = route.path.split('/');

                        // 重新赋值路径
                        route.path = '/category/' + splitPath[2];

                        // 重新赋值组件
                        route.component = function () {
                            return import('./views' + splitPath.join('/'));
                        }

                        // 子路由推入父类别路由
                        categoryRoute.children.push(route);

                        // 在原路由集合中移除路由;
                        routes.splice(i, 1);

                        i--;
                    }
                }

                // 类别路由推入路由集合
                routes.push(categoryRoute);

                // 添加商品列表详情路由

                // 路由器动态添加路由
                router.addRoutes(routes);
            });
        },
    },

    render(h) {
        // 已登入显示总布局组件，未登入显示入口页面组件
        return h(this.isLoggedIn ? layout : entry);
    }
};